// AWTScrollbarPeer.cpp : implementation file
//

#include "stdafx.h"
#include "awt.h"
#include "AWTScrollbarPeer.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_DYNAMIC(AWTScrollbarPeer, CScrollBar)

/////////////////////////////////////////////////////////////////////////////
// AWTScrollbarPeer

AWTScrollbarPeer::AWTScrollbarPeer(AWTComponent* comp)
{
	m_comp = comp;
	m_AWTWindow = NULL;		
}

AWTScrollbarPeer::~AWTScrollbarPeer()
{
	if (m_hWnd != NULL)
	{
		::DestroyWindow(m_hWnd);
	}
}


BEGIN_MESSAGE_MAP(AWTScrollbarPeer, CScrollBar)
	//{{AFX_MSG_MAP(AWTScrollbarPeer)
	ON_WM_CREATE()
	ON_WM_HSCROLL_REFLECT()
	ON_WM_VSCROLL_REFLECT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void AWTScrollbarPeer::setValues(int value, int visibleAmount, int minimum, int maximum)
{
	m_value = value;
	m_visibleAmount = visibleAmount;
	m_min = minimum;
	m_max = maximum;

	SCROLLINFO si = {0};
	si.cbSize = sizeof(SCROLLINFO);
	si.fMask = SIF_ALL;
	si.nMin = m_min;
	si.nMax = m_max;
	si.nPage = visibleAmount;
	si.nPos = value;
	SetScrollInfo(&si);

	m_max -= (m_visibleAmount-1);
}

/////////////////////////////////////////////////////////////////////////////
// AWTScrollbarPeer message handlers

int AWTScrollbarPeer::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CScrollBar::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	m_AWTWindow = getAWTWindow(this);
/*
	CWnd* wnd = this;
	CString str;
	BOOL bFound = FALSE;
	do {
		wnd = wnd->GetParent();
		wnd->GetWindowText(str);
		if (str == "!~AWTWindow~!")
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTDialogPeer)))
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTMdiChildWnd)))
		{
			bFound = TRUE;
			break;
		}
	} while (wnd != NULL);
	if (bFound)
		m_AWTWindow = wnd;		
*/
	return 0;
}

void AWTScrollbarPeer::HScroll(UINT nSBCode, UINT nPos) 
{
	AWTScrollbar* comp = (AWTScrollbar*)m_comp;
	switch (nSBCode)
	{
		case SB_LINERIGHT:
			{
			if (m_value < m_max)
			{
				m_value += m_lineIncrement;
				if (m_value > m_max) m_value = m_max;
				AWTAdjustmentEvent e(m_comp, AWTAdjustmentEvent::BLOCK_INCREMENT, AWTAdjustmentEvent::BLOCK_INCREMENT, m_value);
				comp->processAdjustmentEvent(&e);			
			}
			break;
			}
		case SB_LINELEFT:
			{
			if (m_value > m_min)
			{
				m_value -= m_lineIncrement;
				if (m_value < m_min) m_value = m_min;
				AWTAdjustmentEvent e(m_comp, AWTAdjustmentEvent::BLOCK_DECREMENT, AWTAdjustmentEvent::BLOCK_DECREMENT, m_value);
				comp->processAdjustmentEvent(&e);			
			}
			break;
			}
		case SB_PAGERIGHT:
			{
			if (m_value < m_max)
			{
				m_value += m_pageIncrement;
				if (m_value > m_max) m_value = m_max;
				AWTAdjustmentEvent e(m_comp, AWTAdjustmentEvent::UNIT_INCREMENT, AWTAdjustmentEvent::UNIT_INCREMENT, m_value);
				comp->processAdjustmentEvent(&e);			
			}
			break;
			}
		case SB_PAGELEFT:
			{
			if (m_value > m_min)
			{
				m_value -= m_pageIncrement;
				if (m_value < m_min) m_value = m_min;
				AWTAdjustmentEvent e(m_comp, AWTAdjustmentEvent::UNIT_DECREMENT, AWTAdjustmentEvent::UNIT_DECREMENT, m_value);
				comp->processAdjustmentEvent(&e);			
			}
			break;
		case SB_THUMBPOSITION:
			{
			m_value = nPos;	
			AWTAdjustmentEvent e(m_comp, AWTAdjustmentEvent::ENDTRACK, AWTAdjustmentEvent::ENDTRACK, m_value);
			comp->processAdjustmentEvent(&e);			
			}
			break;
		case SB_THUMBTRACK:
			{
			m_value = nPos;	
			AWTAdjustmentEvent e(m_comp, AWTAdjustmentEvent::TRACK, AWTAdjustmentEvent::TRACK, m_value);
			comp->processAdjustmentEvent(&e);			
			}
			break;
		}
	}
	SetScrollPos(m_value);
}

void AWTScrollbarPeer::VScroll(UINT nSBCode, UINT nPos) 
{
	AWTScrollbar* comp = (AWTScrollbar*)m_comp;
	switch (nSBCode)
	{
		case SB_LINERIGHT:
			{
			if (m_value < m_max)
			{
				m_value += m_lineIncrement;
				if (m_value > m_max) m_value = m_max;
				AWTAdjustmentEvent e(comp, AWTAdjustmentEvent::BLOCK_INCREMENT, AWTAdjustmentEvent::BLOCK_INCREMENT, m_value);
				comp->processAdjustmentEvent(&e);			
			}
			break;
			}
		case SB_LINELEFT:
			{
			if (m_value > m_min)
			{
				m_value -= m_lineIncrement;
				if (m_value < m_min) m_value = m_min;
				AWTAdjustmentEvent e(m_comp, AWTAdjustmentEvent::BLOCK_DECREMENT, AWTAdjustmentEvent::BLOCK_DECREMENT, m_value);
				comp->processAdjustmentEvent(&e);			
			}
			break;
			}
		case SB_PAGERIGHT:
			{
			if (m_value < m_max)
			{
				m_value += m_pageIncrement;
				if (m_value > m_max) m_value = m_max;
				AWTAdjustmentEvent e(m_comp, AWTAdjustmentEvent::UNIT_INCREMENT, AWTAdjustmentEvent::UNIT_INCREMENT, m_value);
				comp->processAdjustmentEvent(&e);			
			}
			break;
			}
		case SB_PAGELEFT:
			{
			if (m_value > m_min)
			{
				m_value -= m_pageIncrement;
				if (m_value < m_min) m_value = m_min;
				AWTAdjustmentEvent e(m_comp, AWTAdjustmentEvent::UNIT_DECREMENT, AWTAdjustmentEvent::UNIT_DECREMENT, m_value);
				comp->processAdjustmentEvent(&e);			
			}
			break;
		case SB_THUMBPOSITION:
			{
			m_value = nPos;	
			AWTAdjustmentEvent e(m_comp, AWTAdjustmentEvent::ENDTRACK, AWTAdjustmentEvent::ENDTRACK, m_value);
			comp->processAdjustmentEvent(&e);			
			}
			break;
		case SB_THUMBTRACK:
			{
			m_value = nPos;	
			AWTAdjustmentEvent e(m_comp, AWTAdjustmentEvent::TRACK, AWTAdjustmentEvent::TRACK, m_value);
			comp->processAdjustmentEvent(&e);			
			}
			break;
		}
	}
	SetScrollPos(m_value);
}
